package com.hos.db.manager;


import com.hos.db.create.ill.IllBuildTable;
import com.hos.db.create.ill.IllCureTable;
import com.hos.db.create.ill.IllDataTable;
import com.hos.db.create.ill.IllHistorysTable;
import com.hos.db.create.ill.IllOptionTable;
import com.hos.db.create.ill.IllResultTable;
import com.hos.db.create.ill.IllsTable;
import com.hos.db.entity.ill.IllBuildEntity;
import com.hos.db.entity.ill.IllCureEvtity;
import com.hos.db.entity.ill.IllDataEntity;
import com.hos.db.entity.ill.IllHistoryEntity;
import com.hos.db.entity.ill.IllOptionEntity;
import com.hos.db.entity.ill.IllResultEntity;
import com.hos.db.entity.ill.IllsEntity;
import com.hos.net.req.ill1.IllAddReq;
import com.hos.net.req.ill1.IllUpdateReq;
import com.hos.net.res.BaseResponse;
import com.hos.net.res.MsgHint;
import com.hos.utile.StringUtile;

import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;

//病例
public class IllsManager1 {

    private static IllsManager1 userPatsManager;

    public static IllsManager1 getInstance() {
        if (userPatsManager == null) {
            userPatsManager = new IllsManager1();
        }
        return userPatsManager;
    }

    /**
     * 添加病例
     *
     * @param jdbcTemplate
     * @param docId
     * @param req
     * @return
     */
    public String onIllAddReq(JdbcTemplate jdbcTemplate, String docId, String docName, IllAddReq req) {
        String response = new MsgHint("添加失败").toJson();
        IllsEntity ill = req.ill;
        ill.setDocId(docId);
        ill.setDocName(docName);
        //添加病历信息
        String illId = IllsTable.onInsert(jdbcTemplate, ill);
        if (StringUtile.isEmpty(illId)) {
            return response;
        }
        boolean isSucced = true;
        if (isSucced) {
            //添加病史
            isSucced = IllHistorysTable.onInsert(jdbcTemplate, illId, req.illHistory);
        }
        if (isSucced) {
            //病历病人体格信息
            isSucced = IllBuildTable.onInsert(jdbcTemplate, illId, req.illBuild);
        }
        if (isSucced) {
            //影像资料
            isSucced = IllDataTable.onInsert(jdbcTemplate, illId, req.illData);
        }
        if (isSucced) {
            //治疗信息
            isSucced = IllCureTable.onInsert(jdbcTemplate, illId, req.illCure);
        }
        if (isSucced) {
            //病情治疗结果/出院情况
            isSucced = IllResultTable.onInsert(jdbcTemplate, illId, req.illResultEntity);
        }
        if (!isSucced) {
            return response;
        }
        //
        BaseResponse res = new BaseResponse();
        ill.setIllId(illId);
        res.obj = req;
        response = res.toJson("0", "添加成功");
        return response;
    }

    public String onIllDeleteReq(JdbcTemplate jdbcTemplate, String illId) {
        //添加病历信息
        boolean isDelete = IllsTable.onDlete(jdbcTemplate, illId);
        if (!isDelete) {
            return new MsgHint("删除失败").toJson();
        }
        //添加病史
        IllHistorysTable.onDlete(jdbcTemplate, illId);
        //病历病人体格信息
        IllBuildTable.onDlete(jdbcTemplate, illId);
        //影像资料
        IllDataTable.onDlete(jdbcTemplate, illId);
        //治疗信息
        IllCureTable.onDlete(jdbcTemplate, illId);
        //病情治疗结果/出院情况
        IllResultTable.onDlete(jdbcTemplate, illId);
        //
        return new MsgHint("0", "删除成功").toJson();
    }

    public String onIllUpdateReq(JdbcTemplate jdbcTemplate, IllUpdateReq req) {
        String illId = req.illId;
        //添加病历信息
        IllsTable.onUpdate(jdbcTemplate, illId, req.ill);
        //添加病史
        IllHistorysTable.onUpdate(jdbcTemplate, illId, req.illHistory);
        //病历病人体格信息
        IllBuildTable.onUpdate(jdbcTemplate, illId, req.illBuild);
        //影像资料
        IllDataTable.onUpdate(jdbcTemplate, illId, req.illData);
        //治疗信息
        IllCureTable.onUpdate(jdbcTemplate, illId, req.illCure);
        //病情治疗结果/出院情况
        IllResultTable.onUpdate(jdbcTemplate, illId, req.illResult);
        //
        return new MsgHint("0", "修改成功").toJson();
    }

    /**
     * @param jdbcTemplate
     * @param patId        患者id
     * @param pageNo       页面（>=1）
     * @param pageSize     每页查询数量
     * @param hasNextPage  判断是否有下一页
     */
    public List<IllsEntity> getIlls(JdbcTemplate jdbcTemplate, String patId, int pageNo, int pageSize,
                                    boolean hasNextPage) {
        List<IllsEntity> ills = IllsTable.getResults(jdbcTemplate, patId, pageNo, pageSize,
                hasNextPage);
        return ills;
    }

    //获取病历
    public IllsEntity getIlls(JdbcTemplate jdbcTemplate, String key) {
        IllsEntity ill = IllsTable.getResults(jdbcTemplate, key);
        return ill;
    }

    //获取病历数量
    public int getIllSize(JdbcTemplate jdbcTemplate, String key) {
        return IllsTable.getSize(jdbcTemplate, key);
    }

    //获取病史
    public IllHistoryEntity getIllHistory(JdbcTemplate jdbcTemplate, String illId) {
        IllHistoryEntity obj = IllHistorysTable.getResults(jdbcTemplate, illId);
        return obj;
    }

    //病历病人体格信息
    public IllBuildEntity getIllBuild(JdbcTemplate jdbcTemplate, String illId) {
        IllBuildEntity obj = IllBuildTable.getResults(jdbcTemplate, illId);
        return obj;
    }

    //影像资料
    public IllDataEntity getIllData(JdbcTemplate jdbcTemplate, String illId) {
        IllDataEntity obj = IllDataTable.getResults(jdbcTemplate, illId);
        return obj;
    }

    //治疗信息
    public IllCureEvtity getCureEvtity(JdbcTemplate jdbcTemplate, String illId) {
        IllCureEvtity obj = IllCureTable.getResults(jdbcTemplate, illId);
        return obj;
    }

    //病情治疗结果/出院情况
    public IllResultEntity getIllResult(JdbcTemplate jdbcTemplate, String illId) {
        IllResultEntity obj = IllResultTable.getResults(jdbcTemplate, illId);
        return obj;
    }

    //获取选项
    public List<IllOptionEntity> getIllOptions(JdbcTemplate jdbcTemplate, String[] keys) {
        List<IllOptionEntity> dats = IllOptionTable.getOptions(jdbcTemplate, keys);
        return dats;
    }
}
